from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from controller.PublicAdminController import PublicAdminController
from controller.StudentAdminController import StudentAdminController
from schema.QuerySchema import QueryBase, StudentQuery
from schema.TeachingSchema import StudentInfo
from util.Auth import check_token
from util.DataBaseTools import get_db_session
from util.ErrorCode import ErrorCode
from util.LogHelper import Log

student_admin_router = APIRouter()


@student_admin_router.post("/student/add", summary='添加学员信息')
def add_student(studentInfo: StudentInfo, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/student/add')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/student/add', db):
            response = StudentAdminController.add_student(studentInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@student_admin_router.post("/student/mod", summary='根据学员id修改学员信息')
def mod_student(studentInfo: StudentInfo, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/student/mod')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/student/mod', db):
            response = StudentAdminController.mod_student(studentInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@student_admin_router.post("/student/querybypage", summary='分页查询学员所有信息')
def query_student_by_page(query_base: QueryBase, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/student/querybypage')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/student/query', db):
            response = StudentAdminController.query_student_by_page(query_base, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@student_admin_router.get("/student/querybyclassid/{class_id}", summary='根据class_id查询学员信息')
def query_student_by_class_id(class_id: int, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/student/querybyclassid')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/student/query', db):
            response = StudentAdminController.query_student_by_class_id(class_id, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@student_admin_router.post("/student/query",
                           summary='根据class_id、name、school_type、has_graduation_cert、status、sale_id、source、pay_type字段分页查询学员信息')
def query_student_by_field(studentQuery: StudentQuery, db: Session = Depends(get_db_session),
                           result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/student/query')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/student/query', db):
            response = StudentAdminController.query_student_by_field(studentQuery, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@student_admin_router.get("/student/querybyid/{student_id}", summary='根据student_id查询学员信息')
def query_student_by_id(student_id: int, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/student/querybyid/{student_id}')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/student/query', db):
            response = StudentAdminController.query_student_by_id(student_id, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@student_admin_router.post("/student/setmoitor/{student_id}/{is_monitor}", summary='根据学员id设置学员为班长')
def set_monitor(student_id: int, is_monitor: int, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/student/setmoitor/')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/student/mod', db):
            response = StudentAdminController.set_monitor(student_id, is_monitor, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result
